package com.mango.iot.gateway.http.consts;

import java.text.MessageFormat;

/**
 * 请求返回对象
 *
 * @author liangfeihu
 * @since 2021/8/20 15:34
 */
public class JsonResult {

    public static final String SUCCESS = "00000";
    public static final String SUCCESS_MSG = "操作成功";
    public static final String FAIL = "99999";

    private String code;

    private String message;

    private Object data;

    public JsonResult() {
    }

    public JsonResult(String code) {
        this.code = code;
    }

    public JsonResult(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public JsonResult(String code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    /**
     * 返回成功消息
     *
     * @return 成功消息
     */
    public static JsonResult success() {
        return JsonResult.success(SUCCESS_MSG);
    }

    /**
     * 返回成功数据
     *
     * @return 成功消息
     */
    public static JsonResult success(Object data) {
        return JsonResult.success(SUCCESS_MSG, data);
    }

    /**
     * 返回成功消息
     *
     * @param msg 返回内容
     * @return 成功消息
     */
    public static JsonResult success(String msg) {
        return JsonResult.success(msg, null);
    }

    /**
     * 返回成功消息
     *
     * @param msg  返回内容
     * @param data 数据对象
     * @return 成功消息
     */
    public static JsonResult success(String msg, Object data) {
        return new JsonResult(SUCCESS, msg, data);
    }

    /**
     * 返回错误消息
     *
     * @param msg 返回内容
     */
    public static JsonResult error(String msg) {
        return JsonResult.error(FAIL, msg, null);
    }

    /**
     * 返回错误消息
     *
     * @param msg
     * @param object
     * @return
     */
    public static JsonResult errorFormat(String msg, Object... object) {
        return JsonResult.error(FAIL, MessageFormat.format(msg, object), null);
    }

    /**
     * 返回错误消息
     *
     * @param code 状态码
     * @param msg  返回内容
     */
    public static JsonResult error(String code, String msg) {
        return JsonResult.error(code, msg, null);
    }

    public static JsonResult error(ResultCode resultCode) {
        return new JsonResult(resultCode.getCode(), resultCode.getMessage(), null);
    }

    /**
     * 返回错误消息
     *
     * @param code 状态码
     * @param msg  返回内容
     * @param data 数据对象
     */
    public static JsonResult error(String code, String msg, Object data) {
        return new JsonResult(code, msg, data);
    }

}
